package com.example.cangku.web.control;

import com.example.cangku.web.dao.WangruqingCangkuMapper;
import com.example.cangku.web.dataobject.WangruqingCangku;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller
public class InformationController {
    @Autowired
    WangruqingCangkuMapper wangruqingCangkuMapper;

    //数据库单条数据插⼊ insert
    @RequestMapping(path = "/insert")
    public String insert(WangruqingCangku cangku, ModelMap modelMap) {
        if (wangruqingCangkuMapper.selectByPrimaryKey(cangku.getId()) != null) {
            return "idError";
        } else {
            wangruqingCangkuMapper.insert(cangku);
            modelMap.addAttribute("insert", cangku);
            return "insertSuccess";
        }
    }

    // 数据库单条数据删除 delete
    @RequestMapping(path = "/delete")
    public String delete(@RequestParam("id") int id,ModelMap modelMap) {
        if (wangruqingCangkuMapper.selectByPrimaryKey(id) == null) {
            return "idError.html";
        } else {
            wangruqingCangkuMapper.deleteByPrimaryKey(id);
            modelMap.addAttribute("delete", id);
            return "deleteSuccess.html";
        }
    }

    @ResponseBody
    @RequestMapping("/query1")
    public String query1(@RequestParam("hang") int hang) {
        if (wangruqingCangkuMapper.selectByHang(hang)==null){
            return "数据库中没有这条数据";
        }
        StringBuilder sb=new StringBuilder();
        List<WangruqingCangku> cangkus= wangruqingCangkuMapper.selectByHang(hang);
        for (WangruqingCangku item:cangkus) {
            sb.append(item.getId()+"  "+item.getHang()+"  "
                    +item.getLie()+"  "+item.getKind()+"  "
                    +item.getTime()+"</br>");
        }

        return sb.toString();
    }


    @ResponseBody
    @RequestMapping("/query2")
    public String query2(@RequestParam("lie") int lie) {
        if (wangruqingCangkuMapper.selectByLie(lie)==null){
            return "数据库中没有这条数据";
        }
        StringBuilder sb=new StringBuilder();
        List<WangruqingCangku> cangkus= wangruqingCangkuMapper.selectByLie(lie);
        for (WangruqingCangku item:cangkus) {
            sb.append(item.getId()+"  "+item.getHang()+"  "
                    +item.getLie()+"  "+item.getKind()+"  "
                    +item.getTime()+"</br>");
        }

        return sb.toString();
    }


    @ResponseBody
    @RequestMapping("/query3")
    public String query3(@RequestParam("kind") String kind) {
        if (wangruqingCangkuMapper.selectByKind(kind)==null){
            return "数据库中没有这条数据";
        }
        StringBuilder sb=new StringBuilder();
        List<WangruqingCangku> cangkus= wangruqingCangkuMapper.selectByKind(kind);
        for (WangruqingCangku item:cangkus) {
            sb.append(item.getId()+"  "+item.getHang()+"  "
                    +item.getLie()+"  "+item.getKind()+"  "
                    +item.getTime()+"</br>");
        }

        return sb.toString();
    }

    @ResponseBody
    @RequestMapping("/list")
    public String list() {
        List<WangruqingCangku> cangku = wangruqingCangkuMapper.selectAll();
        StringBuilder sb=new StringBuilder();
        for (WangruqingCangku item:cangku) {
            sb.append(item.getId()+"  "+item.getHang()+"  "
                    +item.getLie()+"  "+item.getKind()+"  "
                    +item.getTime()+"</br>");
        }
        return sb.toString();
    }


}
